iT邦幫忙

2024 iThome 鐵人賽

DAY 8
0

特殊功能方塊

之前我們做的方塊都是普通方塊,沒有甚麼特殊功能,現在我們可以寫一些程式讓方塊更有用也更有趣

前置作業

我們先向一般的方塊一樣註冊並創建一個新的方塊

    public static final Block KUNLUN_STONE =registerBlock("kunlun_stone",
            new Block(FabricBlockSettings.copyOf(Blocks.IRON_BLOCK)));

然後要記得附上紋理https://ithelp.ithome.com.tw/upload/images/20240906/20161797SHsJwDRvD5.png
還有Blockstate以及models

{
  "variants": {
    "": {
      "model": "como:block/kunlun_stone"
    }
  }
}
{
  "parent": "minecraft:block/cube_all",
  "textures": {
    "all": "como:block/kunlun_stone"
  }
}

https://ithelp.ithome.com.tw/upload/images/20240906/20161797qcBeVd633q.png

特殊效果

我的這個方塊中文降我的這個方塊中文叫崑崙石,我打算給他的效果是[當玩家站在這個方塊上面就會提供生命回復]
為了做到這一點,我們需要知道玩家腳底下是甚麼樣的方塊,並在那個方塊是崑崙石的時候給予玩家回復效果。
首先我們再block資料夾建立一個新的資料夾叫custom代表這是給特殊方塊的資料夾,然後我們建立一個KunlunStone的class

package net.como.block.custom;

import net.como.block.ModBlocks;
import net.minecraft.block.Block;
import net.minecraft.entity.effect.StatusEffectInstance;
import net.minecraft.entity.effect.StatusEffects;
import net.minecraft.entity.player.PlayerEntity;
import net.minecraft.util.math.BlockPos;
import net.minecraft.world.World;

public class KunlunStone extends Block {
    public KunlunStone(Settings settings) {
        super(settings);
    }

    private static Block Kunlun_Stone = ModBlocks.KUNLUN_STONE;

    public static void checkPlayerOnKunlunStone(PlayerEntity player){
        World world =player.getEntityWorld();
        BlockPos pos = player.getBlockPos();

        if (world.getBlockState(pos.down()).getBlock() == Kunlun_Stone){
            if (!player.hasStatusEffect(StatusEffects.REGENERATION)) {
                player.addStatusEffect(new StatusEffectInstance(StatusEffects.REGENERATION,50,0,false,false,true));
            }
        }
    }
}
public class KunlunStone extends Block {
    public KunlunStone(Settings settings) {
        super(settings);
    }

這個class繼承Block,構造函數public KunlunStone(Settings settings)用來初始化方塊。

private static Block Kunlun_Stone = ModBlocks.KUNLUN_STONE;

這裡我們定義靜態變數Kunlun_Stone引用ModBlocks.KUNLUN_STONE

checkPlayerOnKunlunStone(PlayerEntity player)

這個方法接受玩家作為參數,我們透過

World world =player.getEntityWorld();
BlockPos pos = player.getBlockPos();

取得玩家目前所在的世界以及位置

if (world.getBlockState(pos.down()).getBlock() == Kunlun_Stone)

我們使用if判斷如果pos的下方的方塊是Kunlun_Stone,我們就給玩家特殊效果

if (!player.hasStatusEffect(StatusEffects.REGENERATION)) {
                player.addStatusEffect(new StatusEffectInstance(StatusEffects.REGENERATION,50,0,false,false,true));
            }

StatusEffectInstance實例透過StatusEffecT.REGENERATIOM給予玩家再生效果,StatusEffects.REGENERATION代表你想給予的效果,這裡是再生效果,50代表這個效果維持的時間,0代表再生1級,後面三個布林值分別代表是否為隱形效果(類似信號台的效果),是否顯示粒子效果以及是否顯示右上角的藥水ICON。
前面的IF判斷玩家目前是否已經有藥水效果,這樣效果就不會重複疊加,導致再生速度超快。

接下來我們要到模組主類中註冊伺服器每Tick檢查功能,我們再public void onInitialize()中加入ServerTickEvents.END_SERVER_TICK.register(this::onServerTickEnd);

public void onInitialize() {
		ModItemGroups.registerItemGroups();
		Moditems.registerModItems();
		ModBlocks.registerModBlocks();
		OpenCustomGuiPacket.registerReceiver();
		LOGGER.info("Hello Fabric world!");
		FuelRegistry.INSTANCE.add(Moditems.Colophony, 300);
		ServerTickEvents.END_SERVER_TICK.register(this::onServerTickEnd);

	}

END_SERVER_TICK,會在伺服器端的每個Tick(1/20秒)結束時執行一次後面的onServerTickEnd程式,我們程式這樣寫

public void onServerTickEnd(MinecraftServer server) {
		for (PlayerEntity player : server.getPlayerManager().getPlayerList()) {
			PlayeroffHandChecker.checkOffHandItem(player);
			KunlunStone.checkPlayerOnKunlunStone(player);
		}
	}

第一個for迴圈會歷遍將這個伺服器中的所有玩家,我們剛剛寫好的KunlunStone.checkPlayerOnKunlunStone。

成果

Yes


上一篇
Minecraft Fabric Mod 模組製作 DAY7
下一篇
Minecraft Fabric Mod 模組製作 DAY9
系列文
Minecraft JAVA Fabric 模組製作 : 成為真正的創世神28
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言